package com.woyou.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import com.woyou.bean.ChosenGoods;
import com.woyou.bean.Goods;
import com.woyou.bean.IChoose;
import com.woyou.bean.OptionGroup;
import com.woyou.bean.SubGoods;

import android.util.Log;

/**
 * 商品列表UI层业务逻辑封装
 * 
 * @author longtao.li
 * 
 */
@EBean(scope = Scope.Singleton)
public class GoodsModel extends SuperModel {

	/**
	 * 商品列表显示的数据源
	 */
	private List<Object> dataSource = new ArrayList<Object>();
	
	/**
	 * 记录Goods与OptionGoods的从属关系
	 */
	private Map<String, List<ChosenGoods>> ownerMember = new HashMap<String, List<ChosenGoods>>();
	
	/**
	 * 记录选中的Goods
	 */
	private List<Goods> goodsList = new ArrayList<Goods>();

	/**
	 * 记录
	 * 
	 * @param goods
	 */
	public void recordGoods(Goods goods) {
		if (!goodsList.contains(goods)) {
			goodsList.add(goods);
		}
	}

	/**
	 * 删除
	 * 
	 * @param goods
	 */
	public void delGoods(Goods goods) {
		if (goodsList.contains(goods)) {
			goodsList.remove(goods);
		}
	}

	/**
	 * 记录
	 * 
	 * @param cg
	 */
	public void recordChosenGoods(ChosenGoods cg) {
		List<ChosenGoods> list = ownerMember.get(cg.getgId());
		if (list == null) {
			list = new ArrayList<ChosenGoods>();
			ownerMember.put(cg.getgId(), list);
		}
		if (!list.contains(cg)) {
			list.add(cg);
		}
	}

	/**
	 * 删除
	 * 
	 * @param cg
	 */
	public void delChosenGoods(ChosenGoods cg) {
		List<ChosenGoods> list = ownerMember.get(cg.getgId());
		if (list.contains(cg)) {
			list.remove(cg);
		}
	}

	/**
	 * 筛选或搜索商品后匹配数据
	 */
	public void dataMatching() {
		// TODO 优化
		Iterator<Object> iterator = dataSource.iterator();
		while (iterator.hasNext()) {
			Object next = iterator.next();
			if (next instanceof ChosenGoods) {
				iterator.remove();
			}
		}
		List<Object> temp = new ArrayList<Object>();
		for (int i = 0; i < dataSource.size(); i++) {
			Object object = dataSource.get(i);
			temp.add(object);
			Log.i("goodsList", "" + goodsList.size());
			for (Goods goods : goodsList) {
				// 之前是否已经选中
				if (object.equals(goods)) {
					// object = goods;
					int indexOf = temp.indexOf(goods);
					temp.set(indexOf, goods);
				}
				// ----
			}
			List<ChosenGoods> list = ownerMember.get(((Goods) object).getgId());
			if (list != null && list.size() > 0) {
				for (ChosenGoods og : list) {
					temp.add(og);
				}
			}
		}
		dataSource.clear();
		dataSource.addAll(temp);
	}

	@AfterInject
	@Override
	protected void onInit() {
	}

	/**
	 * 获得Goods的下属的OptionGoods列表
	 */
	public List<ChosenGoods> getOptionGoodsListByGId(String gId) {
		List<ChosenGoods> list = ownerMember.get(gId);
		if (list == null) {
			list = new ArrayList<ChosenGoods>();
		}
		return list;
	}

	/**
	 * 获取Goods通过gid
	 */
	public Goods getGoodsByGId(String gId) {

		for (int i = 0; i < dataSource.size(); i++) {
			Object obj = dataSource.get(i);
			if (obj instanceof Goods) {
				Goods goods = (Goods) obj;
				if (gId.equals(goods.getgId())) {
					return goods;
				}
			}
		}
		return null;
	}

	/**
	 * 获得显示的数据源
	 */
	public List<Object> getDataSource() {
		return dataSource;
	}

	/**
	 * 商品是否拥有子属性
	 * 
	 * @return
	 */
	public boolean hasOption(Goods goods) {
		List<OptionGroup> optGList = goods.getOptGList();
		List<SubGoods> subGList = goods.getSubGList();
		OptionGroup specGroup = goods.getSpecGroup();
		int size = 0;
		int size2 = 0;
		if (optGList != null) {
			size = optGList.size();
		}
		if (subGList != null) {
			size2 = subGList.size();
		}
		if (size > 0 || size2 > 0 || specGroup != null) {
			return true;
		}
		return false;
	}

	/**
	 * 商品是否拥有子属性
	 * 
	 * @return
	 */
	public boolean hasOption(IChoose iChoose) {
		Goods goods = getGoodsByGId(iChoose.getgId());
		if (goods != null) {
			return hasOption(goods);
		} else {
			if (iChoose.getChosenOptList().size() > 0 || iChoose.getChosenSGList().size() > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 释放资源
	 */
	public void release() {
		// dataSource.clear();
		for (List<ChosenGoods> cgs : ownerMember.values()) {
			dataSource.removeAll(cgs);
		}
		ownerMember.clear();
		for (Goods goods : goodsList) {
			goods.setChosenNum(0);
		}
		goodsList.clear();
	}

}
